package cn.xxxy.fourm.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.swing.border.Border;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import cn.xxxy.fourm.dao.BoardDao;
import cn.xxxy.fourm.dao.PostDao;
import cn.xxxy.fourm.dao.ReplyPostDao;
import cn.xxxy.fourm.dao.UserDao;
import cn.xxxy.fourm.entity.Board;
import cn.xxxy.fourm.entity.Post;
import cn.xxxy.fourm.entity.ReplyPost;
import cn.xxxy.fourm.entity.User;
import cn.xxxy.fourm.model.BoardDto;
import cn.xxxy.fourm.model.PostDto;
import cn.xxxy.fourm.service.PostService;
@Service@Transactional
public class PostServiceImpl implements PostService{
	@Autowired
	private PostDao postDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private BoardDao boardDao;
	@Autowired
	private ReplyPostDao replyPostDao;
	@Override
	public List<PostDto> listPage(int page, int rows) {
		Pageable pageable = new PageRequest(page-1,rows);
		Page<Post> list=postDao.findAll(pageable);
		List<PostDto> listDto=new ArrayList<>();
		for (Post post : list) {
			PostDto postDto=new PostDto();
			postDto.setPtitle(post.getPtitle());
			postDto.setPcontent(post.getPcontent());
			postDto.setPosttime(post.getPosttime());
			postDto.setPid(post.getPid());
			postDto.setHot(post.getHot());
			postDto.setQuality(post.getQuality());
			postDto.setTop(post.getTop());
			postDto.setComplete(post.getComplete());
			User user=userDao.getOne(post.getUser().getUid());
			postDto.setUid(user.getUid());
			postDto.setUname(user.getUname());
			postDto.setUlevel(user.getLevel());
			Board board=boardDao.getOne(post.getBoard().getBid());
			postDto.setBid(board.getBid());
			postDto.setBname(board.getBname());
			List<ReplyPost> rep=replyPostDao.listById(post.getPid());
			postDto.setList(rep);
			postDto.setReplyCount(rep.size());
			listDto.add(postDto);
		}
		return listDto;
	}
	@Override
	public PostDto getPostDetail(int pid) {
		Post post=postDao.getOne(pid);
		PostDto postDto=new PostDto();
		postDto.setPtitle(post.getPtitle());
		postDto.setPcontent(post.getPcontent());
		postDto.setPosttime(post.getPosttime());
		postDto.setPid(post.getPid());
		postDto.setHot(post.getHot());
		postDto.setQuality(post.getQuality());
		postDto.setTop(post.getTop());
		postDto.setComplete(post.getComplete());
		User user=userDao.getOne(post.getUser().getUid());
		postDto.setUid(user.getUid());
		postDto.setUname(user.getUname());
		postDto.setUlevel(user.getLevel());
		Board board=boardDao.getOne(post.getBoard().getBid());
		postDto.setBid(board.getBid());
		postDto.setBname(board.getBname());
		List<ReplyPost> rep=replyPostDao.listById(post.getPid());
		postDto.setList(rep);
		postDto.setReplyCount(rep.size());
		return postDto;
	}
	@Override
	public List<PostDto> listTopPost() {
		List<Post> topPosts=postDao.findByTop();
		List<PostDto> listDto=new ArrayList<>();
		for (Post post : topPosts) {
			PostDto postDto=new PostDto();
			postDto.setPtitle(post.getPtitle());
			postDto.setPcontent(post.getPcontent());
			postDto.setPosttime(post.getPosttime());
			postDto.setPid(post.getPid());
			postDto.setHot(post.getHot());
			postDto.setQuality(post.getQuality());
			postDto.setTop(post.getTop());
			postDto.setComplete(post.getComplete());
			User user=userDao.getOne(post.getUser().getUid());
			postDto.setUid(user.getUid());
			postDto.setUname(user.getUname());
			postDto.setUlevel(user.getLevel());
			Board board=boardDao.getOne(post.getBoard().getBid());
			postDto.setBid(board.getBid());
			postDto.setBname(board.getBname());
			List<ReplyPost> rep=replyPostDao.listById(post.getPid());
			postDto.setList(rep);
			postDto.setReplyCount(rep.size());
			listDto.add(postDto);
		}
		return listDto;
	}
	@Override
	public List<PostDto> listUnCompletePost() {
		List<Post> topPosts=postDao.findByUnComplete();
		List<PostDto> listDto=new ArrayList<>();
		for (Post post : topPosts) {
			PostDto postDto=new PostDto();
			postDto.setPtitle(post.getPtitle());
			postDto.setPcontent(post.getPcontent());
			postDto.setPosttime(post.getPosttime());
			postDto.setPid(post.getPid());
			postDto.setHot(post.getHot());
			postDto.setQuality(post.getQuality());
			postDto.setTop(post.getTop());
			postDto.setComplete(post.getComplete());
			User user=userDao.getOne(post.getUser().getUid());
			postDto.setUid(user.getUid());
			postDto.setUname(user.getUname());
			postDto.setUlevel(user.getLevel());
			Board board=boardDao.getOne(post.getBoard().getBid());
			postDto.setBid(board.getBid());
			postDto.setBname(board.getBname());
			List<ReplyPost> rep=replyPostDao.listById(post.getPid());
			postDto.setList(rep);
			postDto.setReplyCount(rep.size());
			listDto.add(postDto);
		}
		return listDto;
	}
	@Override
	public List<PostDto> listCompletePost() {
		List<Post> topPosts=postDao.findByComplete();
		List<PostDto> listDto=new ArrayList<>();
		for (Post post : topPosts) {
			PostDto postDto=new PostDto();
			postDto.setPtitle(post.getPtitle());
			postDto.setPcontent(post.getPcontent());
			postDto.setPosttime(post.getPosttime());
			postDto.setPid(post.getPid());
			postDto.setHot(post.getHot());
			postDto.setQuality(post.getQuality());
			postDto.setTop(post.getTop());
			postDto.setComplete(post.getComplete());
			User user=userDao.getOne(post.getUser().getUid());
			postDto.setUid(user.getUid());
			postDto.setUname(user.getUname());
			postDto.setUlevel(user.getLevel());
			Board board=boardDao.getOne(post.getBoard().getBid());
			postDto.setBid(board.getBid());
			postDto.setBname(board.getBname());
			List<ReplyPost> rep=replyPostDao.listById(post.getPid());
			postDto.setList(rep);
			postDto.setReplyCount(rep.size());
			listDto.add(postDto);
		}
		return listDto;
	}
	@Override
	public List<PostDto> listQualityPost() {
		List<Post> topPosts=postDao.findByQuality();
		List<PostDto> listDto=new ArrayList<>();
		for (Post post : topPosts) {
			PostDto postDto=new PostDto();
			postDto.setPtitle(post.getPtitle());
			postDto.setPcontent(post.getPcontent());
			postDto.setPosttime(post.getPosttime());
			postDto.setPid(post.getPid());
			postDto.setHot(post.getHot());
			postDto.setQuality(post.getQuality());
			postDto.setTop(post.getTop());
			postDto.setComplete(post.getComplete());
			User user=userDao.getOne(post.getUser().getUid());
			postDto.setUid(user.getUid());
			postDto.setUname(user.getUname());
			postDto.setUlevel(user.getLevel());
			Board board=boardDao.getOne(post.getBoard().getBid());
			postDto.setBid(board.getBid());
			postDto.setBname(board.getBname());
			List<ReplyPost> rep=replyPostDao.listById(post.getPid());
			postDto.setList(rep);
			postDto.setReplyCount(rep.size());
			listDto.add(postDto);
		}
		return listDto;
	}
	@Override
	public List<PostDto> listNewPost() {
		List<Post> topPosts=postDao.findByNew();
		List<PostDto> listDto=new ArrayList<>();
		for (Post post : topPosts) {
			PostDto postDto=new PostDto();
			postDto.setPtitle(post.getPtitle());
			postDto.setPcontent(post.getPcontent());
			postDto.setPosttime(post.getPosttime());
			postDto.setPid(post.getPid());
			postDto.setHot(post.getHot());
			postDto.setQuality(post.getQuality());
			postDto.setTop(post.getTop());
			postDto.setComplete(post.getComplete());
			User user=userDao.getOne(post.getUser().getUid());
			postDto.setUid(user.getUid());
			postDto.setUname(user.getUname());
			postDto.setUlevel(user.getLevel());
			Board board=boardDao.getOne(post.getBoard().getBid());
			postDto.setBid(board.getBid());
			postDto.setBname(board.getBname());
			List<ReplyPost> rep=replyPostDao.listById(post.getPid());
			postDto.setList(rep);
			postDto.setReplyCount(rep.size());
			listDto.add(postDto);
		}
		return listDto;
	}
	@Override
	public List<PostDto> listHotPost() {
		List<Post> topPosts=postDao.findByHot();
		List<PostDto> listDto=new ArrayList<>();
		for (Post post : topPosts) {
			PostDto postDto=new PostDto();
			postDto.setPtitle(post.getPtitle());
			postDto.setPcontent(post.getPcontent());
			postDto.setPosttime(post.getPosttime());
			postDto.setPid(post.getPid());
			postDto.setHot(post.getHot());
			postDto.setQuality(post.getQuality());
			postDto.setTop(post.getTop());
			postDto.setComplete(post.getComplete());
			User user=userDao.getOne(post.getUser().getUid());
			postDto.setUid(user.getUid());
			postDto.setUname(user.getUname());
			postDto.setUlevel(user.getLevel());
			Board board=boardDao.getOne(post.getBoard().getBid());
			postDto.setBid(board.getBid());
			postDto.setBname(board.getBname());
			List<ReplyPost> rep=replyPostDao.listById(post.getPid());
			postDto.setList(rep);
			postDto.setReplyCount(rep.size());
			listDto.add(postDto);
		}
		return listDto;
	}
	@Override
	public List<PostDto> listMyPostsByUid(int uid) {
		List<Post> myPosts=postDao.listMyPostsByUid(uid);
		List<PostDto> listDto=new ArrayList<>();
		for (Post post : myPosts) {
			PostDto postDto=new PostDto();
			postDto.setPtitle(post.getPtitle());
			postDto.setPcontent(post.getPcontent());
			postDto.setPosttime(post.getPosttime());
			postDto.setPid(post.getPid());
			postDto.setHot(post.getHot());
			postDto.setQuality(post.getQuality());
			postDto.setTop(post.getTop());
			postDto.setComplete(post.getComplete());
			User user=userDao.getOne(post.getUser().getUid());
			postDto.setUid(user.getUid());
			postDto.setUname(user.getUname());
			postDto.setUlevel(user.getLevel());
			Board board=boardDao.getOne(post.getBoard().getBid());
			postDto.setBid(board.getBid());
			postDto.setBname(board.getBname());
			List<ReplyPost> rep=replyPostDao.listById(post.getPid());
			postDto.setList(rep);
			postDto.setReplyCount(rep.size());
			listDto.add(postDto);
		}
		return listDto;
	}
	@Override
	public List<PostDto> listkeySearchPost(String key) {
		List<Post> myPosts=postDao.listkeySearchPost(key);
		return null;
	}
	
}
